package com.ext.aic.apis.user;

import android.content.Context;

import androidx.lifecycle.LifecycleOwner;

import com.ext.aic.apis.ApiDefine;
import com.ext.aic.apis.ApiPaths;
import com.ext.aic.apis.BaseRequest;
import com.ext.aic.apis.RequestResponse;
import com.ext.aic.beans.params.LoginParams;
import com.ext.aic.beans.response.LoginItem;
import com.eyinfo.android_pure_utils.events.Action0;
import com.eyinfo.android_pure_utils.utils.JsonUtils;
import com.hjq.http.EasyHttp;
import com.hjq.http.body.JsonRequestBody;
import com.hjq.http.exception.ResponseException;

import lombok.Getter;
import lombok.Setter;

public class LoginRequest<Extra> extends BaseRequest<LoginParams, Extra> {

    @Getter
    @Setter
    private Action0 successCall;

    @Setter
    @Getter
    private Action0 reAuditCall;

    protected void onLoginSuccess(LoginItem loginResponse, LoginParams loginParams, Extra extra) {

    }

    public void request(Context context, LifecycleOwner lifecycleOwner, LoginParams params, Extra extra) {
        EasyHttp.post(lifecycleOwner)
                .api(new ApiDefine(ApiPaths.login))
                .body(new JsonRequestBody(JsonUtils.toJson(params)))
                .request(new RequestResponse<LoginItem>(context, false) {
                    @Override
                    protected void onSuccess(LoginItem response) {
                        if (response == null) {
                            return;
                        }
                        onLoginSuccess(response, params, extra);
                    }

                    @Override
                    public void onHttpFail(Throwable throwable) {
                        if (throwable instanceof ResponseException) {
                            LoginRequest.super.responseExceptionHandler(throwable, params, extra);
                        }
                    }
                });

    }
}
